<?php
/**
 * Created by PhpStorm.
 * User: man0sions
 * Date: 16/8/29
 * Time: 上午10:54
 */

namespace src\base\api;

/**
 * Class FetchStrategy
 * @package src\base\api
 * @author Luficer.p <81434146@qq.com>
 */
abstract class FetchStrategy
{
    protected $proxy;
    protected $cookie;
    protected $user_agent;
    protected $post_data;
    public $timeout = 5;

    /**
     * @return mixed
     */
    abstract function setOptions();

    /**
     * @param Request $request
     * @return mixed
     */
    public function fetch(Request $request)
    {
        $ch = curl_init();
        if ($this->proxy)
            curl_setopt($ch, CURLOPT_PROXY, $this->proxy);


        curl_setopt($ch, CURLOPT_URL, $request->getUrl());
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);

        if ($this->user_agent)
            curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if ($this->cookie) {
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
        }


        $ret = curl_exec($ch);
        curl_close($ch);
        return $ret;
    }


}